package com.dwd.hrm.usedetail;


import com.alibaba.fastjson.JSON;
import com.dwd.hrm.client.SystemClient;
import com.dwd.hrm.domain.LoginUser;
import com.dwd.hrm.domain.Permission;
import com.dwd.hrm.mapper.LoginUserMapper;

import com.dwd.hrm.mapper.PermissionMapper;
import com.dwd.hrm.util.AjaxResult;
import com.dwd.hrm.vo.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private LoginUserMapper loginUserMapper;

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private SystemClient systemClient;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //从数据库查询通过用户名查询
        LoginUser loginUser = loginUserMapper.selectByUsername(username);
        if (loginUser == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        //查询用户的权限
        List<Permission> permissions = permissionMapper.selectPermissionsByUserId(loginUser.getId());

        List<GrantedAuthority> authorities = new ArrayList<>();
        permissions.forEach(permission -> {
            //把用户的权限添加到authorities
            authorities.add(new SimpleGrantedAuthority(permission.getSn()));
        });
        String userContextJson = null;
        //系统用户
        if (loginUser.getType().intValue()==LoginUser.ADMIN) {
            //查询机构id和机构名字
            AjaxResult ajaxResult = systemClient.loginInfo(loginUser.getId());
            if (!ajaxResult.isSuccess()) {
                throw new UsernameNotFoundException("机构获取失败");
            }
            userContextJson = ajaxResult.getResultObj().toString();
        } else {
            //封装成UserContext 因为其他地方是转为UserContext对象
            UserContext userContext = new UserContext();
            userContext.setUserId(loginUser.getId())
                    .setUserName(loginUser.getUsername());
            String string = JSON.toJSONString(userContext);
            userContextJson = string;
        }


        //封装UserDetails返回
        org.springframework.security.core.userdetails.User user1 = new org.springframework.security.core.userdetails.User(
                userContextJson,
                loginUser.getPassword(),
                loginUser.isEnabled(),
                loginUser.isAccountNonExpired(),
                loginUser.isCredentialsNonExpired(),
                loginUser.isAccountNonLocked(),
                authorities
        );
        return user1;
    }

}
